//
//  MZweizhiView.m
//  民族文化宫
//
//  Created by 何锦坤 on 14-8-14.
//  Copyright (c) 2014年 WANG CUNRUI. All rights reserved.
//

#import "MZweizhiView.h"
#import "SKIndoorMapTransfer.h"
@implementation MZweizhiView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
-(id)initWithIndoorMapImageName:(NSString*)indoorMap Frame:(CGRect)frame;
{
    if (self=[super init]) {
        
        self.bounces = NO;
        self.delegate = self;
        self.backgroundColor = [UIColor whiteColor];
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.frame = frame;
        
        UIImage *map = [UIImage imageNamed:indoorMap];
        _mapView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, map.size.width, map.size.height)];
        _mapView.contentMode = UIViewContentModeScaleAspectFit;
        _mapView.image=map;
        _mapView.userInteractionEnabled = YES;
        [self addSubview:_mapView];
        
        //双击
        
        UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap:)];
        [doubleTapGesture setNumberOfTapsRequired:2];
        [_mapView addGestureRecognizer:doubleTapGesture];
        
        CGFloat minScale = self.frame.size.width/_mapView.frame.size.width;
        [self setMinimumZoomScale:minScale];
        [self setZoomScale:minScale];
        
        
    }
    
    return self;
}

#pragma mark - Zoom methods
-(void)handleDoubleTap:(UIGestureRecognizer*)gesture
{
    CGFloat newScale = self.zoomScale*1.5;
    CGRect zoonRect = [self zoomRectForScale:newScale withCenter:[gesture locationInView:gesture.view]];
    [self zoomToRect:zoonRect animated:YES];
    
}

-(CGRect)zoomRectForScale:(CGFloat)scale withCenter:(CGPoint)center
{
    CGRect zoomRect;
    zoomRect.size.height = self.frame.size.height / scale;
    zoomRect.size.width = self.frame.size.width /scale;
    zoomRect.origin.x = center.x -(zoomRect.size.width/2.0);
    zoomRect.origin.y = center.y - (zoomRect.size.height/2.0);
    return zoomRect;
    
    
}

#pragma mark - UIScrollViewDelegate

-(UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    
    return _mapView;
}

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    [scrollView setZoomScale:scale animated:NO];
}


#pragma mark -UITouch
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    CGPoint touchPoint  = [[touches anyObject] locationInView:_mapView];
    NSValue *touchValue = [NSValue valueWithCGPoint:touchPoint];
    [self performSelector:@selector(performTouchTestArea:)
               withObject:touchValue
               afterDelay:0.1];
    NSLog(@"%f  %f",touchPoint.x,touchPoint.y);
}
- (void)performTouchTestArea:(NSValue *)inTouchPoint
{
    CGPoint aTouchPoint = [inTouchPoint CGPointValue];
    [SKIndoorMapTransfer initWithCoordinate:@"75,173,109,173,109,202,74,202" InPoint:aTouchPoint Inview:_mapView WithTitle:@"少数民族非物质文化遗产展示周" delegate:self];
    [SKIndoorMapTransfer initWithCoordinate:@"214,165,236,165,236,186,214,186" InPoint:aTouchPoint Inview:_mapView WithTitle:@"宫藏文物精华展" delegate:self];
    
    [SKIndoorMapTransfer initWithCoordinate:@"332,115,355,114,356,132,332,133" InPoint:aTouchPoint Inview:_mapView WithTitle:@"2014年古代精品面具展" delegate:self];
    [SKIndoorMapTransfer initWithCoordinate:@"118,171,143,171,144,193,119,193" InPoint:aTouchPoint Inview:_mapView WithTitle:@"党的群众路线档案展览" delegate:self];
    
    [SKIndoorMapTransfer initWithCoordinate:@"128,265,151,265,152,285,128,284" InPoint:aTouchPoint Inview:_mapView WithTitle:@"少数民族地区民族服饰展" delegate:self];
    
    [SKIndoorMapTransfer initWithCoordinate:@"239,288,262,288,263,307,240,308" InPoint:aTouchPoint Inview:_mapView WithTitle:@"国家古代“钱币”巡展" delegate:self];
    
    [SKIndoorMapTransfer initWithCoordinate:@"74,99,97,100,98,119,75,120" InPoint:aTouchPoint Inview:_mapView WithTitle:@"2014年《中华玉器展》" delegate:self];
    
}


@end
